﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Threading;
using System.Web.UI.WebControls;

namespace Thksoft.Funds
{
    public partial class BankSetEdit : BasePage
    {
        private YX_BANK_LISTBLL BlistBll = new YX_BANK_LISTBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDate.Value = DateTime.Now.ToString("yyyy-MM-dd");

                BindBank();

                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    BindData();
                }
            }
        }

        /// <summary>
        /// 修改绑定数据
        /// </summary>
        public void BindData()
        {
            string id = Request.QueryString["ID"].ToString();

            YX_BANK_DETAILLIST model = BlistBll.SelectById(id);

            if (model != null)
            {
                FORM_BANK_ID.SelectedValue = model.N_BANK_ID.ToString();
                txtZnum.Text = model.C_ACCNO1;
                txtDfzh.Text = model.C_ACCNO2;
                txtDfzhName.Text = model.C_ACC_NAME1;
                txtamont1.Text = model.N_AMT.ToString();
                txtDate.Value = model.D_TRANDATE.ToString("yyyy-MM-dd");
                txtBanknum.Text = model.C_PROWEEKNUM;
                ddlCheckType.SelectedValue = model.N_FLAG1.ToString();
                txtRemark.Text = model.C_DET;

                DataTable dt = new Yx_AccountBLL().SeleByWhr(" and ACCOUNTNO=" + model.C_ACCNO1);

                if (dt != null && dt.Rows.Count > 0)
                {
                    FORM_TYPE.SelectedValue = dt.Rows[0]["ACCOUNTTYPE"].ToString();

                    FORM_COMPANY_ID.Value = decimal.Parse(dt.Rows[0]["COMPANY_ID"].ToString());
                }
            }
        }

        public void BindBank()
        {
            int count;

            var dt = new Sys_BankBLL().GetEntitys(null, null, 0, 1000, out count);

            FORM_BANK_ID.DataSource = dt;
            FORM_BANK_ID.DataTextField = "C_NAME";
            FORM_BANK_ID.DataValueField = "N_ID";
            FORM_BANK_ID.DataBind();
            FORM_BANK_ID.Items.Insert(0, new ListItem("---请选择---", "-1"));

            FORM_BANK_ID.SelectedValue = "-1";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (FORM_BANK_ID.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择银行");
                return;
            }
            if (txtZnum.Text.Trim() == "")
            {
                MessageBox.Show(Page, "主账号不能为空");
                return;
            }

            if (txtDfzh.Text.Trim() == "")
            {
                MessageBox.Show(Page, "对方账号不能为空");
                return;
            }

            if (txtamont1.Text.Trim() == "")
            {
                MessageBox.Show(Page, "发生额不能为空");
                return;
            }
            else
            {
                decimal je = 0;
                if (!decimal.TryParse(txtamont1.Text.Trim(), out je))
                {
                    MessageBox.Show(Page, "发生额必须是数字");
                    return;
                }
            }

            if (txtDate.Value.Trim() == "")
            {
                MessageBox.Show(Page, "发生时间不能为空");
                return;
            }

            bool mainName = SelectMethod();
            if (mainName == false)
            {
                MessageBox.Show(Page, "主账户要与银行账户设置相匹配!");
                return;
            }

            YX_BANK_DETAILLIST model = new YX_BANK_DETAILLIST();

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //修改
                string id = Request.QueryString["ID"].ToString();

                model = BlistBll.SelectById(id);
            }
            else
            {
                model.N_LIST_ID = 1;//N_DETAIL_LISTID
            }

            model.D_TRANDATE = DateTime.Parse(txtDate.Value);
            model.N_BANK_ID = decimal.Parse(FORM_BANK_ID.SelectedValue);
            model.C_ACCNO1 = txtZnum.Text.Trim();
            model.C_ACCNO2 = txtDfzh.Text.Trim();
            model.C_ACC_NAME1 = txtDfzhName.Text.Trim();
            model.N_AMT = decimal.Parse(txtamont1.Text);
            model.D_TRANDATE = DateTime.Parse(txtDate.Value);
            model.C_PROWEEKNUM = txtBanknum.Text.Trim();
            model.C_DET = txtRemark.Text.Trim();
            model.N_FLAG1 = decimal.Parse(ddlCheckType.SelectedValue);

            //model.D_BANKREDATE = model.D_TRANDATE;

            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                //修改
                int count = BlistBll.UPDATE2(model, lognUser.USER_NAME);

                if (count > 0)
                {
                    MessageBox.Show(Page, "修改成功！");
                    Thread.Sleep(1000 * 5);
                    MessageBox.ResponseScript(this, "FORM_CLOSE();");
                }
                else
                {
                    MessageBox.Show(Page, "修改失败！");
                }
            }
            else
            {
                model.D_BANKREDATE = model.D_TRANDATE;
                //新增
                int count = BlistBll.INSERT(model, lognUser.USER_NAME);

                if (count > 0)
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Funds/BankListEdit.aspx");
                    //MessageBox.Show(Page, "新增成功！");
                }
                else
                {
                    MessageBox.Show(Page, "新增失败！");
                }
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_CLOSE();");
        }

        /// <summary>
        /// 根据输入的主账号查询银行账户设置的主账号
        /// </summary>
        private bool SelectMethod()
        {
            BaseBLL bsBLL = new BaseBLL();
            DataTable dt = null;
            string mainName = this.txtZnum.Text.ToString();

            string sql = " and a.accountno = '" + mainName + "'";

            {
                string _sql = @"select a.accounti_id accountId,
       c.cmy_name companyName,
       d.deptname depName,
       decode(a.accounttype, 1, '支出户', 2, '收入户') accType,
       a.accountno accountNo,
       b.c_name openBank
  from sys_company c, sys_department d, yx_account a,sys_bank b
 where c.cmy_id = d.cmy_id
   and c.cmy_id = a.company_id
   and d.deptid = a.department_id
   and a.n_bank_id=b.n_id";
                if (_sql != "")
                {
                    _sql += sql;
                }
                dt = bsBLL.Query(_sql);
            }

            if (dt.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void BindAccount()
        {
            if (FORM_BANK_ID.SelectedValue.ToString() == "-1")
            {
                return;
            }
            if (FORM_COMPANY_ID.Value != -1)
            {
                string comid = FORM_COMPANY_ID.Value.ToString();

                string bankid = FORM_BANK_ID.SelectedValue;

                string type = FORM_TYPE.SelectedValue;

                string whr = " and COMPANY_ID=" + comid + " and N_BANK_ID=" + bankid + " and ACCOUNTTYPE=" + type;

                DataTable dt = new Yx_AccountBLL().SeleByWhr(whr);

                if (dt != null && dt.Rows.Count > 0)
                {
                    txtZnum.Text = dt.Rows[0]["ACCOUNTNO"].ToString();
                }
            }
        }

        protected void FORM_BANK_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindAccount();
        }

        protected void FORM_TYPE_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindAccount();
        }
    }
}